package com.pudu.mydemo.utils;


import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import com.pudu.mydemo.entity.LogFileThread;

/**
 * Created by lgq on 2016/8/15.
 */
public class LogPrint {

    /**
     * 日志文件总开关
     **/
    private static final boolean LOG_SWITCH = true;
    /**
     * Log日志的tag String : TAG
     */
    public static ArrayList<String> taglist = new ArrayList<>();
    /**
     * 时间格式
     **/
    private static final SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");


    /**
     * 无异常对象情况下输出Log
     *
     * @param logLevel
     * @param tagString
     * @param explainString
     */
    protected static void println(int logLevel, String tagString, String explainString) {
        if (LOG_SWITCH) {
            Log.println(logLevel, tagString, explainString);
        }
    }

    /**
     * 有异常对象情况下输出Log
     *
     * @param logLevel
     * @param tagString
     * @param explainString
     * @param throwable
     */
    protected static void println(int logLevel, String tagString, String explainString, Throwable throwable) {
        switch (logLevel) {
            case LogConfig.VERBOSE:
                Log.v(tagString, explainString, throwable);
                break;
            case LogConfig.DEBUG:
                Log.d(tagString, explainString, throwable);
                break;
            case LogConfig.INFO:
                Log.i(tagString, explainString, throwable);
                break;
            case LogConfig.WARN:
                Log.w(tagString, explainString, throwable);
                break;
            case LogConfig.ERROR:
                Log.e(tagString, explainString, throwable);
                break;
        }
    }

    /**
     * 生成上传服务器日志格式
     */
    protected static String createMessageToServer(Throwable throwable) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("time" + "=" + mSimpleDateFormat.format(new Date()) + "\r\n");
        //stringBuilder.append(getDeviceInfo(paramContext));

        if (throwable == null)
            return stringBuilder.toString();

        Writer mWriter = new StringWriter();
        PrintWriter mPrintWriter = new PrintWriter(mWriter);
        throwable.printStackTrace(mPrintWriter);
        // paramThrowable.printStackTrace();
        Throwable mThrowable = throwable.getCause();
        // 迭代栈队列把所有的异常信息写入writer中
        while (mThrowable != null) {
            mThrowable.printStackTrace(mPrintWriter);
            // 换行 每个个异常栈之间换行
            mPrintWriter.append("\r\n");
            mThrowable = mThrowable.getCause();
        }
        // 记得关闭
        mPrintWriter.close();
        String mResult = mWriter.toString();
        stringBuilder.append(mResult);
        return stringBuilder.toString();
    }

    /**
     * 保存日志到文件
     *
     * @param flag
     * @param explainString
     */
    protected static void saveLogToFile(String tagString,int flag, String explainString) {
        StringBuilder message = new StringBuilder();
        message.append("time" + "=" + mSimpleDateFormat.format(new Date()) + "\r\n");
        message.append(LogConfig.getLogLevelName(flag) + "------" + explainString + "\n");
        if (LOG_SWITCH) {
        /*-----------------------------以下调用 具体保存日志到本地---------------------*/
            Date nowtime = new Date();
            SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
            /**  needWriteFiel是文件日期的名字，获取当前现在的年月日:2016-08-15 + tagString **/
            String needWriteFiel = logfile.format(nowtime)+tagString;
            /**
             * 写入文件日志格式;2016-08-15 18:26:47  4  helloworld
             * 【时间】+level+【打印的内容】
             */
            String needWriteMessage = mSimpleDateFormat.format(nowtime) + "\t" + flag + "\t" + explainString;
            /**
             * 文件命名格式：[时间]+Log.txt   2016-08-15Log.txt
             * 也就是每天会生成一份Log.txt文件,当前的日志追加在同一txt文件中
             */
        //taglist 是将所有的tag添加进集合里面，为了在上传文件取得文件的名字
            taglist.add(tagString);
            //handler发送要写入的日志到线程
            if (LogFileThread.getmHandler() != null) {
                Message msg = LogFileThread.getmHandler().obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("tagString", needWriteFiel);
                bundle.putInt("flag", flag);
                bundle.putString("explainString", needWriteMessage);
                msg.setData(bundle);
                msg.sendToTarget();
            }else{
                Log.i("LogPrint","LooperThread.getmHandler()==null---"+LogFileThread.getmHandler());
            }

            Log.println(Log.INFO, "Log保存到文件", message.toString());
        }
    }

    /**
     * 保存日志到文件
     *
     * @param flag
     * @param explainString
     */
    protected static void saveLogToFile(String tagString,int flag, String explainString, Throwable throwable) {
        StringBuilder message = new StringBuilder();
        message.append("time" + "=" + mSimpleDateFormat.format(new Date()) + "\r\n");
        message.append(LogConfig.getLogLevelName(flag) + "------" + explainString + "\n");
        message.append(createMessageToServer(throwable));

        if (LOG_SWITCH) {
        /*-----------------------------以下调用 具体保存本地方法---------------------*/
            Date nowtime = new Date();
            SimpleDateFormat logfile = new SimpleDateFormat("yyyy-MM-dd");// 日志文件格式
            /**  needWriteFiel是文件日期的名字，获取当前现在的年月日:2016-08-15 + tagString **/
            String needWriteFiel = logfile.format(nowtime)+tagString;
            /**
             * 写入文件日志格式;2016-08-15 18:26:47  4  helloworld
             * 【时间】+level+【打印的内容】
             */
            String needWriteMessage = mSimpleDateFormat.format(nowtime) + "\t" + flag + "\t" + explainString + "\t" + createMessageToServer(throwable) ;
            /**
             * 文件命名格式：[时间]+Log.txt   2016-08-15Log.txt
             * 也就是每天会生成一份Log.txt文件,当前的日志追加在同一txt文件中
             */
            //开启子线程来写入文件内容

            //handler发送要写入的日志到线程
            if (LogFileThread.getmHandler() != null) {
                Message msg = LogFileThread.getmHandler().obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putString("tagString", needWriteFiel);
                bundle.putInt("flag", flag);
                bundle.putString("explainString", needWriteMessage);
                msg.setData(bundle);
                msg.sendToTarget();
            }else{
                Log.i("LogPrint","LooperThread.getmHandler()==null---"+LogFileThread.getmHandler());
            }

            Log.println(Log.INFO, "Log保存到文件", message.toString());
        }

    }


    public ArrayList<String> getTaglist() {
        return taglist;
    }

}
